package com.java.comprehensive.arithmetic.leecode.easy;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LeetCode_349 {
    public static int[] intersection(int[] nums1, int[] nums2) {
        IntStream stream = Arrays.stream(nums1);
        Stream<Integer> integerStream = stream.boxed();
        Set<Integer> set = integerStream.collect(Collectors.toSet());//不推荐，大小固定不能增删
        Set<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                hashSet.add(nums2[i]);
            }
        }
        int[] result = new int[hashSet.size()];
        int j = 0;
        for (Integer integer : hashSet) {
            result[j] = integer;
            j++;
        }
        return result;
    }

    public static int[] intersection2(int[] nums1, int[] nums2) {
        Set<Integer> set = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
        return Arrays.stream(nums2).distinct().filter(set::contains).toArray();
    }


    public static void main(String[] args) {

    }
}
